#!/bin/bash
# Version:1.0.0
# Author:modongxiao
# Description:nginx install
# chmod +x nginx-install.sh

# 错误监测
# 参数（在执行后执行函数，并传入错误信息）
# 第一个参数：固定 $?
# 第二个参数：错误信息
# 例如：check_error $? "执行错误"
function check_error {
     if [ $1 -ne 0 ] ; then
        echo $2",请检查！";
        exit 1
    fi
    return 0;
}

case "$1" in
    install)
        if [ -d "/usr/local/nginx" ] ; then
            echo "nginx可能已经安装，请查看 /usr/local/ 目录下的 nginx 目录";
            exit 1
        fi
        if [ ! -d "/data/log/nginx" ] ; then
            mkdir -pv /data/log/nginx
            echo "日志目录创建完成";
        fi
        if [ ! -d "/var/cache/nginx/client_temp" ] ; then
            mkdir -pv /var/cache/nginx/client_temp
        fi
        yum -y install wget tar
        yum -y install gcc gcc-c++  pcre-devel  openssl-devel
        check_error $? "yum 安装依赖包错误"

        # 下载 与 编译安装
        id nginx || useradd -r nginx
        mkdir nginx
        cd nginx
        wget http://172.18.202.1/source/nginx/nginx-1.10.0.tar.gz
        check_error $? "安装包下载错误"
        tar -axf nginx-1.10.0.tar.gz
        cd nginx-1.10.0/
        check_error $? "安装包路径错误"
        ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-file-aio --with-http_v2_module --with-ipv6
        check_error $? "configure 编译错误"
        make -j 4
        check_error $? "make 编译错误"
        make install
        check_error $? "make install 编译错误"
        echo "编译安装完成...开始配置..."

        # 配置
        echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh;
        source /etc/profile.d/nginx.sh
        rm -rf  /etc/nginx/nginx.conf
        wget http://172.18.202.1/source/nginx/conf/nginx.conf -P /etc/nginx/
        rm -rf  /etc/nginx/nginx.d/magedu.conf
        wget http://172.18.202.1/source/nginx/conf/magedu.conf -P /etc/nginx/nginx.d/
        [ ! -f /etc/rc.d/init.d/nginx ] && wget http://172.18.202.1/source/nginx/conf/nginx -P /etc/rc.d/init.d/
        chmod +x /etc/rc.d/init.d/nginx
        chkconfig --add nginx
        chkconfig --level 345 nginx on
        # 启动
        service nginx start
        check_error $? "nginx 启动错误"
        echo "nginx 启动完成!";
    ;;
    delete)
        service nginx stop
        rm -rf /usr/local/nginx/
        rm -rf /etc/nginx/
        rm -rf /etc/rc.d/init.d/nginx
        echo '卸载完成 ! '
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {install|start|shutdown|reboot|delete|}'
        exit 1
    ;;
esac


